package com.newproject.resmi;

import java.util.LinkedList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Stakan extends ListActivity {

	private ProgressDialog instr_ProgressDialog = null;
	private LinkedList<StockOffer> stock_offers = null;
	private StockOfferAdapter stockoffer_adapter;
	private Runnable viewStockOffers;
	private String idKase;
	private String typeGlass;
	private volatile boolean stopStakanUpdate = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stakan);

		Bundle bundle = this.getIntent().getExtras();
		setTitle(bundle.getString("shortName"));
		this.idKase = bundle.getString("idKase");
		this.typeGlass = bundle.getString("typeGlass");
		
		this.stock_offers = new LinkedList<StockOffer>();
		this.stockoffer_adapter = new StockOfferAdapter(this,
				R.layout.stakan_row, stock_offers);
		setListAdapter(this.stockoffer_adapter);

		viewStockOffers = new Runnable() {
			// @Override
			public void run() {
				while (true) {
					try {
						if (!stopStakanUpdate) {
							fillStockOffers();
						}
						Thread.sleep(10000);
					} catch (InterruptedException e) {
						Log.e("BACKGROUND_PROC", e.getMessage());
					}
				}
			}
		};
		Thread thread = new Thread(null, viewStockOffers, "MagentoBackground");
		thread.start();
		instr_ProgressDialog = ProgressDialog.show(Stakan.this,
				this.getString(R.string.wait),
				this.getString(R.string.loading), true);
	}

	@Override
	public void onResume() { 
		super.onResume();
		stopStakanUpdate = false;

	}

	@Override
	public void onPause() {
		super.onPause();
		stopStakanUpdate = true;
	}

	private Runnable returnRes = new Runnable() {
		// @Override
		public void run() {
			if (stock_offers != null && stock_offers.size() > 0) {
				stockoffer_adapter.notifyDataSetChanged();
			}else if(stock_offers.size() == 0){
				//GeneralHelper.showAlert(Stakan.this, getString(R.string.stakan_no_data));
				AlertDialog.Builder builder = new AlertDialog.Builder(Stakan.this);
		    	builder.setMessage(getString(R.string.stakan_no_data))
		    	       .setCancelable(true)
		               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int id) {
		                         dialog.cancel();
		                         Stakan.this.finish();
		                    }
		                });
		    	AlertDialog alert = builder.create();
		    	alert.show();
			}
			instr_ProgressDialog.dismiss();
			
		}
	};

	private void fillStockOffers() {		
		DataManager.fillStockOffers(stock_offers, this.idKase, this.typeGlass);
		runOnUiThread(returnRes);
	}

	private class StockOfferAdapter extends ArrayAdapter<StockOffer> {
		private LinkedList<StockOffer> items;

		public StockOfferAdapter(Context context, int textViewResourceId,
				LinkedList<StockOffer> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); // TODO:
																										// read
																										// about
																										// getSystemService
				v = vi.inflate(R.layout.stakan_row, null);
			}
			StockOffer o = items.get(position);
			if (o != null) {
				TextView sellVolume = (TextView) v
						.findViewById(R.id.SellVolume);
				TextView price = (TextView) v.findViewById(R.id.Price);
				TextView buyVolume = (TextView) v.findViewById(R.id.BuyVolume);
				if (sellVolume != null) {
					sellVolume.setText(NumberFormatter.formatInt(o
							.getSellVolume()));
				}
				if (price != null) {
					price.setText(NumberFormatter.formatFloat(o.getPrice()));
				}
				if (buyVolume != null) {
					buyVolume.setText(NumberFormatter.formatInt(o
							.getBuyVolume()));
				}
				Resources res = getResources();
				if (o.getBest() == 1) {
					sellVolume.setTextColor(res.getColor(R.color.red));
					price.setTextColor(res.getColor(R.color.red));
					buyVolume.setTextColor(res.getColor(R.color.red));
					// sellVolume.setTypeface(sellVolume.getTypeface(),
					// Typeface.BOLD);
					// price.setTypeface(price.getTypeface(), Typeface.BOLD);
					// buyVolume.setTypeface(buyVolume.getTypeface(),
					// Typeface.BOLD);
				} else if (o.getBest() == 0){
					sellVolume.setTextColor(res.getColor(R.color.light_gray));
					price.setTextColor(res.getColor(R.color.light_gray));
					buyVolume.setTextColor(res.getColor(R.color.light_gray));
				} else {
					sellVolume.setTextColor(res.getColor(R.color.light_turquoise));
					price.setTextColor(res.getColor(R.color.light_turquoise));
					buyVolume.setTextColor(res.getColor(R.color.light_turquoise));
				}
			}
			return v;
		}
	}
}
